/*
 * Created by SharpDevelop.
 * User: Oscar
 * Date: 29/09/2007
 * Time: 08:32 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Text.RegularExpressions;

namespace DemoPress.UrlRewriting
{
	/// <summary>
	/// Description of UrlRewriteRule.
	/// </summary>
	public class UrlRewriteRule
	{
		private string _source;
		private string _destination;
		private UrlRewriteSectionHandler _handler;
		private Regex _regex;
		
		private UrlRewriteSectionHandler Handler
		{
			get { return _handler; }
		}
		private Regex Regex {
			get { return _regex; }
		}
		
		public string Source {
			get { return _source; }
		}
		
		public string Destination {
			get { return _destination; }
		}
		
		public UrlRewriteRule(string source, string destination, UrlRewriteSectionHandler handler)
		{
			_source = source;
			_destination = destination;
			_handler = handler;
			_regex = new Regex(handler.RewriteBase + source, RegexOptions.IgnoreCase);
		}
		
		public bool MatchSource(string path)
		{
			Match match = this.Regex.Match(path);
			return match.Success;
		}
		
		public string RewritePath(string path)
		{
			return this.Regex.Replace(path, this.Destination);
		}
	}
}
